/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ipark.gui.mapeditor;

import java.awt.*;
import java.io.Serializable;
import javax.swing.ImageIcon;

/**
 * Parking box component
 * @author jernej
 */
public class ParkingBox extends MapComponent  implements Serializable{
    
    private transient Image[] images = null;// = new Image[10];
    private int typeParkBox = 0;
    private int orientParkBox = 0;
    private String[] parkingBoxOrient = {"Vertical","Horizontal","Diagonal"};
    private String[] pbvhTypes = {"Ordinary","Disabled"};
    private String[] pbdTypes = {"Top Right","Top Left","Bottom Left","Bottom Right","Top Right Disabled","Top Left Disabled"};
    private String section = "";
    
    public ParkingBox(ParkingBox comp){
      this();
      setLocation(comp.getLocation());
      setSize(comp.getSize());
      setName(comp.getName());
      setWidthMeters(comp.getWidthMeters());
      setHeightMeters(comp.getHeightMeters());
      typeParkBox = comp.typeParkBox;
      orientParkBox = comp.orientParkBox;
      section = comp.section;
    }
    
    public ParkingBox(){
      super();
      loadImages();
      try {
           MediaTracker tracker = new MediaTracker(this);
           for(int i=0;i<images.length;i++)
                tracker.addImage(images[i], 0);
           tracker.waitForID(0);
      } catch (Exception e) {}
      setSize(images[0].getWidth(this),images[0].getHeight(this));
      setName("ParkingBox");
      setWidthMeters(3);
      setHeightMeters(5.5);
  }

    public void loadImages(){
        if(images==null){
            images = new Image[10];
            images[0] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/parkingbox1.png"));
            images[1] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/paraparkingbox1.png"));
            images[2] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/parkingbox2.png"));
            images[3] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/paraparkingbox2.png"));
            images[4] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/parkingbox3.png"));
            images[5] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/parkingbox4.png"));
            images[6] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/parkingboxes3.png"));
            images[7] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/parkingboxes4.png"));
            images[8] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/paraparkingbox3.png"));
            images[9] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/paraparkingbox4.png"));
        }
    }
    @Override
    public ImageIcon getIcon(int iWidth, int iHeight) {
        return new ImageIcon(images[0].getScaledInstance(iWidth,iHeight, Image.SCALE_SMOOTH));
    }
    
    public static String getInfoName(){
      return "Default";
  }

    @Override
    public boolean isHorizontalResizable() {
        return true;
    }

    @Override
    public boolean isVerticalResizable() {
        return true;
    }

    @Override
    public boolean isDiagonalResizable() {
        return false;
    }

    @Override
    public double minHeightMeters() {
        return 1;
    }

    @Override
    public double minWidthMeters() {
        return 1;
    }

    @Override
    public String[] getPropertyNames() {
        String[] ret = {"Name","Width","Height","Orientation","Type","Section"};
        return ret;
    }

    @Override
    public Object getPropertyValue(String name) {
      if(name.equals("Name"))
          return getName();
      if(name.equals("Width"))
          return Math.floor(getWidthMeters()*100)/100;
      if(name.equals("Height"))
          return Math.floor(getHeightMeters()*100)/100;
      if(name.equals("Type")){
          switch(orientParkBox){
                case 0:
                case 1:
                    return pbvhTypes[typeParkBox];
                case 2:
                    return pbdTypes[typeParkBox];
            }
          return null;
      }
      if(name.equals("Orientation"))
          return parkingBoxOrient[orientParkBox];
      if(name.equals("Section"))
          return section;
      return null;
    }

    @Override
    public boolean setPropertyValue(String name, String value) {
        double temp;
      if(name.equals("Name")){
          setName(value);
          return true;
      }
      if(name.equals("Width")){
          try{
            temp = Double.parseDouble(value);
          }catch(NumberFormatException e){return false;}
          temp = Math.floor(temp*100)/100;
          if(temp<0.01 && temp >= minWidthMeters())
              return false;
          setWidthMeters(temp);
          setSize((int)(getWidthMeters()/getMetersPerPixel()),getHeight());
          return true;
      }
      if(name.equals("Height")){
          try{
            temp = Double.parseDouble(value);
          }catch(NumberFormatException e){return false;}
          temp = Math.floor(temp*100)/100;
          if(temp<0.01 && temp >= minHeightMeters())
              return false;
          setHeightMeters(temp);
          setSize(getWidth(),(int)(getHeightMeters()/getMetersPerPixel())); 
          return true;
      }
      if(name.equals("Type")){
          String[] types = null;
          switch(orientParkBox){
                case 0:
                case 1:
                    types = pbvhTypes;
                break;
                case 2:
                    types = pbdTypes;
            }
          for(int i=0;i<types.length;i++)
              if(types[i].equals(value)){
                  typeParkBox = i;
                  return true;
              }
          return false;
      }
      if(name.equals("Orientation")){
          typeParkBox=0;
          for(int i=0;i<parkingBoxOrient.length;i++)
              if(parkingBoxOrient[i].equals(value)){
                  if(orientParkBox!=i && (i==1 || orientParkBox==1)){
                      temp = getWidthMeters();
                      setWidthMeters(getHeightMeters());
                      setHeightMeters(temp);
                      setSize((int)(getWidthMeters()/getMetersPerPixel()),
                              (int)(getHeightMeters()/getMetersPerPixel()));
                  }
                  orientParkBox = i;
                  return true;
              }
          return false;
      }
      if(name.equals("Section")){
          section = value;
          return true;
      }
      return false;
    }
    
    @Override
    public void paint(Graphics g){
        super.paint(g);
        //izrišemo sliko, ki predstavlja ta objekt
        Image image = images[0];
        switch(orientParkBox){
             case 0:
                 image = images[typeParkBox];
             break;
             case 1:
                 image = images[typeParkBox+2];
             break;
             case 2:
                 image = images[typeParkBox+4];
             break;
        }
        g.drawImage(image, getLocation().x, getLocation().y,getLocation().x+getWidth(),
                    getLocation().y+getHeight(), 0, 0, image.getWidth(this), image.getHeight(this), this);
    }

    @Override
    public String[] getTypeNames(String property) {
        if(property.equals("Type")){
            switch(orientParkBox){
                case 0:
                case 1:
                    return pbvhTypes;
                case 2:
                    return pbdTypes;
            }
        }
        if(property.equals("Orientation"))
            return parkingBoxOrient;
        else
            return null;
    }
    
    @Override
    public MapComponent duplicate() {
        MapComponent ret = new ParkingBox(this);
        return ret;
    }

    
    @Override
    public boolean jointSide(int side) {
        return false;
    }
    
}
